clear
set more off
pause on
set matsize 10000

******************************************************************************************
* Name: 	regs-election.do
* Purpose: 	this do file uses merged election data to run regressions, make graphs
* Input:	ca-election.dta 
******************************************************************************************

*****************************
* Set locals	
*****************************

* Option to use data with both candidates in a race present
local both = 0

* Create a folder to store the results
local suffix = "both`both'"
local tabs = "tabs-`suffix'"
local gph = "gph-`suffix'"
cap mkdir `tabs'
cap mkdir `gph'

cap log close
log using regs-election-`suffix'.log, text replace

*****************************
* Sample definition
*****************************

use ca-election, clear

desc
summ

assert first_election!=.
assert officecat!=.
assert sex!=.

if `both'==1 {
	* limit to races with both winner/loser in a given race part of the sample (non-incumbent, first observed)
	duplicates tag first_raceid if  first_margin!=., g(tag_both_multi)	
	
	keep if  tag_both_multi==1
	
	local folder = "both-cand"
	count
}

* Define regression controls: first election year, county
egen county = group(CNTYNAME)
local controls = "i.first_election i.county"

* Define clustering
local cluster_var = "county"

* Merge in female representation at the county and county-office level, from 1995-7
merge m:1 CNTYNAME officecat2 using female-rep.dta
tab _m
drop _m
 
* Drop variables
keep first* female run_atall run8_atall win* margin_next run_again county* officecat officecat2 cityname citypop high* mean* count* 

**********************************
* Variable definition 
**********************************

* Variable definition
gen left = first_margin<0
gen right = first_margin>0 & first_margin!=.
gen first_margin_right = first_margin*right
gen first_margin_left = first_margin*left

gen marginsq = first_margin*first_margin
gen margin_rightsq = marginsq*right
gen margin_leftsq = marginsq*left

gen win_any_uncond = win_any
replace win_any_uncond = 0 if run_atall==0

gen win8_any_uncond = win8_any
replace win8_any_uncond = 0 if run8_atall==0

* Interact female with cutoff
gen female_margin = female*first_margin_left 
gen female_marginsq = female*margin_leftsq
gen female_margin_right = female*first_margin_right
gen female_margin_rightsq = female*margin_rightsq

gen first_loss = 1 if first_elected==0
replace first_loss = 0 if first_elected==1
gen female_loss = female*first_loss

label var first_loss "Lost"
label var female_loss "Female*Lost"

label var first_elected "Elected"
label var first_margin_left "Margin of victory"
label var first_margin_right "Margin of victory*Elected"
label var margin_leftsq "(Margin of victory)^2"
label var margin_rightsq "(Margin of victory*Elected)^2"

* office, occupation, city dummies
tab officecat2, g(office_group)

rename first_occ_group occ_group
tab occ_group, g(occ_group_cat)
label val occ_group occ_group
label var occ_group_cat1 "management"
label var occ_group_cat2 "business/financial operations"
label var occ_group_cat3 "computer/mathematical"
label var occ_group_cat4 "architecture/engineering"
label var occ_group_cat5 "life/physical/social science"
label var occ_group_cat6 "community/social service"
label var occ_group_cat7 "legal"
label var occ_group_cat8 "education/training/library"
label var occ_group_cat9 "arts/design/entertainment/sports/media"
label var occ_group_cat10 "healthcare practitioners"
label var occ_group_cat11 "healthcare support"
label var occ_group_cat12 "protective service"
label var occ_group_cat13 "food prep/serving related"
label var occ_group_cat14 "building/grounds cleaning and maintenance"
label var occ_group_cat15 "personal care/service"
label var occ_group_cat16 "sales"
label var occ_group_cat17 "office/admin support"
label var occ_group_cat18 "farming/fishing/forestry"
label var occ_group_cat19 "construction/extraction"
label var occ_group_cat20 "installation/maintenance/repair"
label var occ_group_cat21 "production"
label var occ_group_cat22 "transportation"
label var occ_group_cat23 "material moving"
*label var occ_group_cat24 "military"
label var occ_group_cat24 "student"
label var occ_group_cat25 "govt employee"
label var occ_group_cat26 "business owner"
label var occ_group_cat27 "parent/homemaker"
label var occ_group_cat28 "retired"
label var occ_group_cat29 "not classified"
label var occ_group_cat30 "none listed"

* Median population designation for city council offices
egen citygroup = group(cityname) if officecat==3
bys citygroup: gen c=_n if citygroup!=.
summ citypop if c==1, det
gen medcity = citypop>r(p50) if citypop!=. & officecat==3
 
tab medcity, m

****************************
* Parametric regressions 
****************************

local main_vars = "first_loss first_margin_left first_margin_right"
local main_vars_sq = "first_loss first_margin_left margin_leftsq first_margin_right margin_rightsq"
local main_vars_f = "female_loss first_loss female first_margin_left female_margin first_margin_right female_margin_right" 
local main_vars_f_sq = "female_loss first_loss female first_margin_left female_margin margin_leftsq female_marginsq first_margin_right female_margin_right margin_rightsq female_margin_rightsq"

****************************
* Tables 2, 3, 6, A.7, A.9
****************************

foreach y in run_atall win_uncond win_any_uncond run8_atall win8_uncond win8_any_uncond {

		* Linear
		reg `y' `main_vars' `controls', cluster(`cluster_var') 
		summ `y' if first_loss==0 & e(sample)	
		outreg2 using `tabs'/rdpara-`y'.xls, excel ctitle("All") dec(3) addstat(Mean Winners, r(mean)) label replace 
		
		* Quadratic
		reg `y' `main_vars_sq' `controls' , cluster(`cluster_var') 
		summ `y' if first_loss==0 & e(sample)	
		outreg2 using `tabs'/rdpara-`y'.xls, excel ctitle("All") dec(3) addstat(Mean Winners, r(mean)) label append
		
		* Linear, female
		reg `y' `main_vars' `controls' if female==1, cluster(`cluster_var') 
		summ `y' if first_loss==0 & e(sample)	
		outreg2 using `tabs'/rdpara-`y'.xls, excel ctitle("Female") dec(3) addstat(Mean Winners, r(mean)) label append
		
		* Quadratic, female
		reg `y' `main_vars_sq' `controls' if  female==1, cluster(`cluster_var') 
		summ `y' if first_loss==0 & e(sample)	
		outreg2 using `tabs'/rdpara-`y'.xls, excel ctitle("Female") dec(3) addstat(Mean Winners, r(mean)) label append
		
		* Linear, male
		reg `y' `main_vars' `controls' if female==0, cluster(`cluster_var') 
		summ `y' if first_loss==0 & e(sample)	
		outreg2 using `tabs'/rdpara-`y'.xls, excel ctitle("Male") dec(3) addstat(Mean Winners, r(mean)) label append
		
		* Quadratic, male
		reg `y' `main_vars_sq' `controls' if  female==0, cluster(`cluster_var') 
		summ `y' if first_loss==0 & e(sample)	
		outreg2 using `tabs'/rdpara-`y'.xls, excel ctitle("Male") dec(3) addstat(Mean Winners, r(mean)) label append
		
		* compare male/female coefficients, linear
		reg `y' `main_vars' `controls' if  female==1
		est store regf
		reg `y' `main_vars' `controls' if  female==0
		est store regm
		suest regf regm, cluster(`cluster_var')
		test [regf_mean]first_loss=[regm_mean]first_loss
		
		* compare male/female coefficients, quadratic
		reg `y' `main_vars_sq' `controls' if  female==1
		est store regf
		reg `y' `main_vars_sq' `controls' if  female==0
		est store regm
		suest regf regm, cluster(`cluster_var')
		test [regf_mean]first_loss=[regm_mean]first_loss
		
		* Interact female 
		reg `y' `main_vars_f' `controls' , cluster(`cluster_var') 
		summ `y' if first_loss==0 & e(sample)	
		outreg2 using `tabs'/rdpara-`y'-female.xls, excel ctitle("All") dec(3) addstat(Mean Winners, r(mean)) label replace
		reg `y' `main_vars_f_sq' `controls' , cluster(`cluster_var') 
		summ `y' if first_loss==0 & e(sample)	
		outreg2 using `tabs'/rdpara-`y'-female.xls, excel ctitle("All") dec(3) addstat(Mean Winners, r(mean)) label append
}

if `both'==0 {
************************
* Covariate Balance 
* Tables A.2, A.3, A.4
************************

estimates clear 

gen first_numvotes = abs(first_totalvotes*first_voteshare)

foreach y of varlist office_group* medcity first_numvotes {

		* Linear
		reg `y' `main_vars' `controls' , cluster(`cluster_var')  
		summ `y' if first_loss==0 & e(sample)	
		outreg2 using `tabs'/rdpara-`y'.xls, excel ctitle("All") dec(3) addstat(Mean Winners, r(mean)) label replace 
		
		reg `y' `main_vars' `controls' 
		est store `y'_1
		
		* Quadratic
		reg `y' `main_vars_sq' `controls' , cluster(`cluster_var') 
		summ `y' if first_loss==0 & e(sample)	
		outreg2 using `tabs'/rdpara-`y'.xls, excel ctitle("All") dec(3) addstat(Mean Winners, r(mean)) label append
		
		reg `y' `main_vars_sq' `controls' 
		est store `y'_2

		* Linear, female
		reg `y' `main_vars' `controls' if  female==1, cluster(`cluster_var') 
		summ `y' if first_loss==0 & e(sample)	
		outreg2 using `tabs'/rdpara-`y'.xls, excel ctitle("Female") dec(3) addstat(Mean Winners, r(mean)) label append

		reg `y' `main_vars' `controls' if  female==1
		est store `y'_1_f
		
		* Quadratic, female
		reg `y' `main_vars_sq' `controls' if  female==1, cluster(`cluster_var') 
		summ `y' if first_loss==0 & e(sample)	
		outreg2 using `tabs'/rdpara-`y'.xls, excel ctitle("Female") dec(3) addstat(Mean Winners, r(mean)) label append
		
		reg `y' `main_vars_sq' `controls' if  female==1
		est store `y'_2_f

		* Linear, male
		reg `y' `main_vars' `controls' if  female==0, cluster(`cluster_var') 
		summ `y' if first_loss==0 & e(sample)	
		outreg2 using `tabs'/rdpara-`y'.xls, excel ctitle("Male") dec(3) addstat(Mean Winners, r(mean)) label append

		reg `y' `main_vars' `controls' if  female==0
		est store `y'_1_m
					
		* Quadratic, male
		reg `y' `main_vars_sq' `controls' if  female==0, cluster(`cluster_var') 
		summ `y' if first_loss==0 & e(sample)	
		outreg2 using `tabs'/rdpara-`y'.xls, excel ctitle("Male") dec(3) addstat(Mean Winners, r(mean)) label append

		reg `y' `main_vars_sq' `controls' if  female==0
		est store `y'_2_m
}

* joint test
forval x=1/2 {

	suest office_group*_`x', cluster(`cluster_var')
	test first_loss
	
}

foreach g in m f {
	
	forval x=1/2 {

		suest office_group*_`x'_`g', cluster(`cluster_var')
		test first_loss

	}
}

*****************************
* Start in 1998 
* Table A.5
****************************

foreach y of varlist run_atall {

		* Interact female with polynomials
		reg `y' `main_vars_f' `controls' if  first_election>1997, cluster(`cluster_var') 
		summ `y' if first_loss==0 & e(sample)	
		outreg2 using `tabs'/rdpara-`y'-female-start1998.xls, excel ctitle("All") dec(3) addstat(Mean Winners, r(mean)) label replace

		reg `y' `main_vars_f_sq' `controls' if  first_election>1997, cluster(`cluster_var') 
		summ `y' if first_loss==0 & e(sample)	
		outreg2 using `tabs'/rdpara-`y'-female-start1998.xls, excel ctitle("All") dec(3) addstat(Mean Winners, r(mean)) label append
}


****************************
* Identity of opponent
* Table A.6
****************************

* (1) men who ran initially against women
* (2) men who ran initially against men
* (3) women who ran initially against women
* (4) women who ran initially against men

forval x=0/1 {

	forval y=0/1 {
	
		if `x'==0 & `y'==0 local desc = "MM"
		if `x'==0 & `y'==1 local desc = "MF"
		if `x'==1 & `y'==0 local desc = "FM"
		if `x'==1 & `y'==1 local desc = "FF"
	
		* Male running against a male
		if `x'==0 & `y'==0 local samp = "(female==0 & first_opponent_sex==0)"
		* Female running against a female
		if `x'==1 & `y'==1 local samp = "(female==1 & first_opponent_sex==1)"	
		* Male running against a female: male loser
		if `x'==0 & `y'==1 local samp = "((female==0 & first_opponent_sex==1 & first_elected==0) | (female==1 & first_opponent_sex==0 & first_elected==1))"
		* Male running against a female: female loser
		if `x'==1 & `y'==0 local samp = "((female==1 & first_opponent_sex==0 & first_elected==0) | (female==0 & first_opponent_sex==1 & first_elected==1))"	
	
		foreach z in run_atall {		

			* Linear
			reg `z' `main_vars' `controls' if  `samp', cluster(`cluster_var') 
			summ `z' if first_loss==0 & e(sample)
			if `x'==0 & `y'==0 outreg2 using `tabs'/rdpara-`z'-pairs.xls, excel ctitle("`desc'") dec(3) addstat(Mean Winners, r(mean)) label replace 
			if !(`x'==0 & `y'==0) outreg2 using `tabs'/rdpara-`z'-pairs.xls, excel ctitle("`desc'") dec(3) addstat(Mean Winners, r(mean)) label append

			* Quadratic
			reg `z' `main_vars_sq' `controls' if  `samp', cluster(`cluster_var') 
			summ `z' if first_loss==0 & e(sample)
			outreg2 using `tabs'/rdpara-`z'-pairs.xls, excel ctitle("`desc'") dec(3) addstat(Mean Winners, r(mean)) label append

		}
	}
}	

* Combine MF and FM 
local samp = "( (female==1 & first_opponent_sex==0) | (female==0 & first_opponent_sex==1) )"	

foreach z in run_atall {	
	* Linear
	reg `z' `main_vars_f' `controls' if  `samp', cluster(`cluster_var') 
	summ `z' if first_loss==0 & e(sample)
	outreg2 using `tabs'/rdpara-`z'-pairs.xls, excel ctitle("MF FM") dec(3) addstat(Mean Winners, r(mean)) label append

	* Quadratic
	reg `z' `main_vars_f_sq' `controls' if  `samp', cluster(`cluster_var') 
	summ `z' if first_loss==0 & e(sample)
	outreg2 using `tabs'/rdpara-`z'-pairs.xls, excel ctitle("MF FM") dec(3) addstat(Mean Winners, r(mean)) label append

}
 
**********************
* Office Composition
* Table 5 
**********************

foreach y in run_atall {

		* Interact office with loss and running var
		reg `y' `main_vars_f' i.officecat i.officecat#first_loss i.officecat#c.first_margin_left i.officecat#c.first_margin_right `controls' , cluster(`cluster_var') 
		outreg2 using `tabs'/rdpara-`y'-female-office-interact.xls, excel ctitle("All") dec(3) label replace
		
		reg `y' `main_vars_f_sq' i.officecat i.officecat#first_loss i.officecat#c.first_margin_left i.officecat#c.first_margin_right ///
			i.officecat#c.margin_leftsq i.officecat#c.margin_rightsq `controls' , cluster(`cluster_var') 
		outreg2 using `tabs'/rdpara-`y'-female-office-interact.xls, excel ctitle("All") dec(3) label append

}
 
****************************
* Occupational Composition 
* Table 5
****************************

foreach y in run_atall {

		* Interact occ with loss and running var
		reg `y' `main_vars_f' i.occ_group i.occ_group#first_loss i.occ_group#c.first_margin_left i.occ_group#c.first_margin_right `controls' , cluster(`cluster_var') 
		outreg2 using `tabs'/rdpara-`y'-female-occ-interact.xls, excel ctitle("All") dec(3) label replace
		
		reg `y' `main_vars_f_sq' i.occ_group i.occ_group#first_loss i.occ_group#c.first_margin_left i.occ_group#c.first_margin_right ///
			i.occ_group#c.margin_leftsq i.occ_group#c.margin_rightsq `controls' , cluster(`cluster_var') 
		outreg2 using `tabs'/rdpara-`y'-female-occ-interact.xls, excel ctitle("All") dec(3) label append
} 
 
****************************************
* Above/below median pop city council 
* Table A.8
****************************************

foreach y in run_atall  {
	
		forval c=0/1 {

			* Interact female with polynomials
			reg `y' `main_vars_f' `controls' if   officecat==3 & medcity==`c', cluster(`cluster_var') 
			summ `y' if first_loss==0 & e(sample)
			outreg2 using `tabs'/rdpara-`y'-medcity`c'-female.xls, excel ctitle("All") dec(3) addstat(Mean Winners, r(mean))  label replace
			
			reg `y' `main_vars_f_sq' `controls' if   officecat==3 & medcity==`c', cluster(`cluster_var') 
			summ `y' if first_loss==0 & e(sample)
			outreg2 using `tabs'/rdpara-`y'-medcity`c'-female.xls, excel ctitle("All") dec(3) addstat(Mean Winners, r(mean))  label append

 		}
}

**********************************************************
* Female representation
* Table 4
**********************************************************

local count = 4

bys high_female_office: tab officecat if count_female_county_office>`count' & first_margin!=.

foreach y in run_atall {
	
		forval c=0/1 {

			* Interact female with polynomials
			reg `y' `main_vars_f' `controls' if   count_female_county_office>`count' & high_female_office==`c', cluster(`cluster_var') 
			summ `y' if first_loss==0 & e(sample)
			outreg2 using `tabs'/rdpara-`y'-femalerep`c'-female.xls, excel ctitle("All") dec(3) addstat(Mean Winners, r(mean))  label replace
			
			reg `y' `main_vars_f_sq' `controls' if   count_female_county_office>`count' & high_female_office==`c', cluster(`cluster_var') 
			summ `y' if first_loss==0 & e(sample)
			outreg2 using `tabs'/rdpara-`y'-femalerep`c'-female.xls, excel ctitle("All") dec(3) addstat(Mean Winners, r(mean))  label append

 		}
}

* From 1998 on
bys high_female_office: tab officecat if count_female_county_office>`count' & first_margin!=. & first_election>1997

foreach y in run_atall {
	
		forval c=0/1 {

			* Interact female with polynomials
			reg `y' `main_vars_f' `controls' if   count_female_county_office>`count' & high_female_office==`c' & first_election>1997, cluster(`cluster_var') 
			summ `y' if first_loss==0 & e(sample)
			outreg2 using `tabs'/rdpara-`y'-femalerep`c'-female1998.xls, excel ctitle("All") dec(3) addstat(Mean Winners, r(mean))  label replace
			
			reg `y' `main_vars_f_sq' `controls' if   count_female_county_office>`count' & high_female_office==`c' & first_election>1997, cluster(`cluster_var') 
			summ `y' if first_loss==0 & e(sample)
			outreg2 using `tabs'/rdpara-`y'-femalerep`c'-female1998.xls, excel ctitle("All") dec(3) addstat(Mean Winners, r(mean))  label append

 		}
}

***********************************
* Heterogeneity by office 
* Table 4
***********************************

foreach y in run_atall {
	
		forval off=1/5 {
		
			if `off'==1 local c = "County Supervisor"
			if `off'==2 local c = "Mayor"
			if `off'==3 local c = "City Council"
			if `off'==4 local c = "Other City Position"
			if `off'==5 local c = "School Board/Superintendent"												
		
			* Interact female
			reg `y' `main_vars_f' `controls' if   officecat==`off', cluster(`cluster_var') 
			summ `y' if first_loss==0 & e(sample)
			if `off'==1 outreg2 using `tabs'/rdpara-`y'-female-byoffice.xls, excel ctitle("`c'") dec(3) addstat(Mean Winners, r(mean)) label replace
			if `off'!=1 outreg2 using `tabs'/rdpara-`y'-female-byoffice.xls, excel ctitle("`c'") dec(3) addstat(Mean Winners, r(mean)) label append			
			
			reg `y' `main_vars_f_sq' `controls' if   officecat==`off', cluster(`cluster_var') 
			summ `y' if first_loss==0 & e(sample)
			outreg2 using `tabs'/rdpara-`y'-female-byoffice.xls, excel ctitle("`c'") dec(3) addstat(Mean Winners, r(mean)) label append
		
		}
}
}

****************************
* Local linear regressions
****************************

**********************
* kernel: uniform
* with controls
**********************

* Tables 2, 3, 6, A.9
foreach y in run_atall win_uncond win_any_uncond {

	rdrobust `y' first_margin, kernel(uni) vce(cluster `cluster_var')
	local bw_all = round(e(h_l), 0.001)

	rdrobust `y' first_margin if female==0, kernel(uni) vce(cluster `cluster_var')
	local bw_m = round(e(h_l), 0.001)
	rdrobust `y' first_margin if female==1, kernel(uni) vce(cluster `cluster_var')
	local bw_f = round(e(h_l), 0.001)
	
	local bw2_all = 2*`bw_all'
	local bwhalf_all = 0.5*`bw_all'

	local bw2_m = 2*`bw_m'
	local bwhalf_m = 0.5*`bw_m'

	local bw2_f = 2*`bw_f'
	local bwhalf_f = 0.5*`bw_f'
	
	foreach h in bw bw2 bwhalf {
	
		local range = "first_margin>-``h'_all' & first_margin<``h'_all' "
	
		* All
		reg `y' `main_vars' `controls' if  `range', cluster(`cluster_var')
		summ `y' if first_loss==0 & e(sample)
		if "`h'"=="bw" outreg2 using `tabs'/rdllr-`y'-controls-bygender.xls, excel ctitle("All ``h'_all'") dec(3) label addstat(Mean Winners, r(mean)) replace 
		if "`h'"!="bw" outreg2 using `tabs'/rdllr-`y'-controls-bygender.xls, excel ctitle("All ``h'_all'") dec(3) label addstat(Mean Winners, r(mean)) append 

		* Interaction
		reg `y' `main_vars_f' `controls' if  `range', cluster(`cluster_var')
		summ `y' if first_loss==0 & e(sample)	
		if "`h'"=="bw" outreg2 using `tabs'/rdllr-`y'-controls-interact.xls, excel ctitle("Interact threshold ``h'_all'") dec(3) label addstat(Mean Winners, r(mean)) replace 	
		if "`h'"!="bw" outreg2 using `tabs'/rdllr-`y'-controls-interact.xls, excel ctitle("Interact all ``h'_all'") dec(3) label addstat(Mean Winners, r(mean)) append 
	}
	
	* Male
	foreach h in bw bw2 bwhalf {
	
		local range_m = "first_margin>-``h'_m' & first_margin<``h'_m' "
		local range_f = "first_margin>-``h'_f' & first_margin<``h'_f' "
	
		reg `y' `main_vars' `controls' if  `range_m' & female==0, cluster(`cluster_var')
		summ `y' if first_loss==0 & e(sample)
		outreg2 using `tabs'/rdllr-`y'-controls-bygender.xls, excel ctitle("Male ``h'_m'") dec(3) label addstat(Mean Winners, r(mean)) append 
	}
	
	* Female
	foreach h in bw bw2 bwhalf {
	
		local range_m = "first_margin>-``h'_m' & first_margin<``h'_m' "
		local range_f = "first_margin>-``h'_f' & first_margin<``h'_f' "
	
		reg `y' `main_vars' `controls' if  `range_f' & female==1, cluster(`cluster_var')
		summ `y' if first_loss==0 & e(sample)
		outreg2 using `tabs'/rdllr-`y'-controls-bygender.xls, excel ctitle("Female ``h'_f'") dec(3) label addstat(Mean Winners, r(mean)) append 
		
		* test equality of male/female coeffs
		reg `y' `main_vars' `controls' if  `range_f' & female==1
		est store regf
		reg `y' `main_vars' `controls' if  `range_m' & female==0
		est store regm
		suest regf regm, cluster(`cluster_var')
		test [regf_mean]first_loss=[regm_mean]first_loss
	}		

}

if `both'==0 {
**************************************************
* 8 years from initial election
* kernel: uniform
* with controls
**************************************************

* Table A.7
foreach y in run8_atall win8_uncond win8_any_uncond {

	rdrobust `y' first_margin, kernel(uni) vce(cluster `cluster_var')
	local bw_all = round(e(h_l), 0.001)
	
	local bw2_all = 2*`bw_all'
	local bwhalf_all = 0.5*`bw_all'
	
	foreach h in bw bw2 bwhalf {
	
		local range = "first_margin>-``h'_all' & first_margin<``h'_all' "
	
		* Interaction
		reg `y' `main_vars_f' `controls' if  `range', cluster(`cluster_var')
		summ `y' if first_loss==0 & e(sample)	
		if "`h'"=="bw" outreg2 using `tabs'/rdllr-`y'-controls-interact.xls, excel ctitle("Interact threshold ``h'_all'") dec(3) label addstat(Mean Winners, r(mean)) replace 	
		if "`h'"!="bw" outreg2 using `tabs'/rdllr-`y'-controls-interact.xls, excel ctitle("Interact all ``h'_all'") dec(3) label addstat(Mean Winners, r(mean)) append 

	}
}

**************************************************
* Omit 1995-1997 from analysis
* kernel: uniform
* with controls
* Table A.5
**************************************************

foreach y in run_atall {

	rdrobust `y' first_margin if first_election>1997, kernel(uni) vce(cluster `cluster_var')
	local bw_all = round(e(h_l), 0.001)
	
	local bw2_all = 2*`bw_all'
	local bwhalf_all = 0.5*`bw_all'
	
	foreach h in bw bw2 bwhalf {
	
		local range = "first_margin>-``h'_all' & first_margin<``h'_all' & first_election>1997"
	
		* Interaction
		reg `y' `main_vars_f' `controls' if  `range', cluster(`cluster_var')
		summ `y' if first_loss==0 & e(sample)	
		if "`h'"=="bw" outreg2 using `tabs'/rdllr-`y'-start1998-interact.xls, excel ctitle("Interact threshold ``h'_all'") dec(3) label addstat(Mean Winners, r(mean)) replace 	
		if "`h'"!="bw" outreg2 using `tabs'/rdllr-`y'-start1998-interact.xls, excel ctitle("Interact all ``h'_all'") dec(3) label addstat(Mean Winners, r(mean)) append 
		
	}
}


**********************
* Office Composition 
* Table 5
**********************

foreach y in run_atall  {

		rdrobust `y' first_margin, kernel(uni) vce(cluster `cluster_var')
		local bw = round(e(h_l), 0.001)

		local bw2 = 2*`bw'
		local bwhalf = 0.5*`bw'

		local h = `bw'

		local range = "first_margin>-`h' & first_margin<`h'"

		* Interact office with loss and running var
		reg `y' `main_vars_f' i.officecat i.officecat#first_loss i.officecat#c.first_margin_left i.officecat#c.first_margin_right `controls' if  `range', cluster(`cluster_var') 
		outreg2 using `tabs'/rdllr-`y'-female-office-interact.xls, excel ctitle("All") dec(3) label replace
		
}
 

****************************
* Occupational Composition
* Table 5 
****************************

foreach y in run_atall  {

		rdrobust `y' first_margin, kernel(uni) vce(cluster `cluster_var')
		local bw = round(e(h_l), 0.001)

		local bw2 = 2*`bw'
		local bwhalf = 0.5*`bw'

		local h = `bw'

		local range = "first_margin>-`h' & first_margin<`h'"
		
		* Interact occ with loss and running var
		reg `y' `main_vars_f' i.occ_group i.occ_group#first_loss i.occ_group#c.first_margin_left i.occ_group#c.first_margin_right `controls' if  `range', cluster(`cluster_var') 
		outreg2 using `tabs'/rdllr-`y'-female-occ-interact.xls, excel ctitle("All") dec(3) label replace
		
} 
 
*****************************************
* Above/below median pop. city council
* Table A.8 
*****************************************

rdrobust run_atall first_margin if medcity!=., kernel(uni) vce(cluster `cluster_var')
local bw = round(e(h_l), 0.001)

foreach y in run_atall   {
	
		if "`y'"=="run_atall" local h = `bw'
		local range = "first_margin>-`h' & first_margin<`h'"

		forval c=0/1 {
			
			* Interact female with polynomials
			reg `y' `main_vars_f' `controls' if  `range' & officecat==3 & medcity==`c', cluster(`cluster_var') 
			summ `y' if first_loss==0 & e(sample)
			outreg2 using `tabs'/rdllr-`y'-medcity`c'-female.xls, excel ctitle("All") dec(3) addstat(Mean Winners, r(mean))  label replace
			
 		}
}

*********************************************************************
* Female representation
* Table 4
*********************************************************************

local count = 4

bys high_female_office: tab officecat if count_female_county_office>`count'

* with all years
rdrobust run_atall first_margin if high_female_office!=. & count_female_county_office>`count', kernel(uni) vce(cluster `cluster_var')
local bw = round(e(h_l), 0.001)

foreach y in run_atall   {
	
		if "`y'"=="run_atall" local h = `bw'
		local range = "first_margin>-`h' & first_margin<`h' & high_female_office!=. & count_female_county_office>`count'"

		forval c=0/1 {

			* Interact female with polynomials
			reg `y' `main_vars_f' `controls' if  `range' & high_female_office==`c', cluster(`cluster_var') 
			summ `y' if first_loss==0 & e(sample)
			outreg2 using `tabs'/rdllr-`y'-femalerep`c'-female.xls, excel ctitle("All") dec(3) addstat(Mean Winners, r(mean))  label replace
			
 		}
}

* with just 1998 onward
bys high_female_office: tab officecat if count_female_county_office>`count' & first_election>1997

rdrobust run_atall first_margin if high_female_office!=. & first_election>1997 & count_female_county_office>`count', kernel(uni) vce(cluster `cluster_var')
local bw = round(e(h_l), 0.001)

foreach y in run_atall   {
	
		if "`y'"=="run_atall" local h = `bw'
		local range = "first_margin>-`h' & first_margin<`h' & high_female_office!=.  & first_election>1997 & count_female_county_office>`count'"

		forval c=0/1 {

			* Interact female with polynomials
			reg `y' `main_vars_f' `controls' if  `range' & high_female_office==`c', cluster(`cluster_var') 
			summ `y' if first_loss==0 & e(sample)
			outreg2 using `tabs'/rdllr-`y'-femalerep`c'-female-start1998.xls, excel ctitle("All") dec(3) addstat(Mean Winners, r(mean))  label replace
			
 		}
}


*****************************
* Identity of opponent
* Table A.6
*****************************

* (1) men who ran initially against women
* (2) men who ran initially against men
* (3) women who ran initially against women
* (4) women who ran initially against men

forval x=0/1 {

	forval y=0/1 {
	
		if `x'==0 & `y'==0 local desc = "MM"
		if `x'==0 & `y'==1 local desc = "MF"
		if `x'==1 & `y'==0 local desc = "FM"
		if `x'==1 & `y'==1 local desc = "FF"
	
		* Male running against a male
		if `x'==0 & `y'==0 local samp = "(female==0 & first_opponent_sex==0)"
		* Female running against a female
		if `x'==1 & `y'==1 local samp = "(female==1 & first_opponent_sex==1)"	
		* Male running against a female: male loser
		if `x'==0 & `y'==1 local samp = "((female==0 & first_opponent_sex==1 & first_elected==0) | (female==1 & first_opponent_sex==0 & first_elected==1))"
		* Male running against a female: female loser
		if `x'==1 & `y'==0 local samp = "((female==1 & first_opponent_sex==0 & first_elected==0) | (female==0 & first_opponent_sex==1 & first_elected==1))"	
		
		foreach z in run_atall {		
			
			rdrobust `z' first_margin if `samp', kernel(uni) vce(cluster `cluster_var')
			local h = round(e(h_l), 0.001)
			
			local range = "first_margin>-`h' & first_margin<`h'"

			reg `z' `main_vars'  `controls' if  `range' & `samp', cluster(`cluster_var') 
			summ `z' if first_loss==0 & e(sample)
			if `x'==0 & `y'==0 outreg2 using `tabs'/rdllr-`z'-pairs.xls, excel ctitle("`desc'") dec(3) addstat(Mean Winners, r(mean)) label replace 
			if !(`x'==0 & `y'==0) outreg2 using `tabs'/rdllr-`z'-pairs.xls, excel ctitle("`desc'") dec(3) addstat(Mean Winners, r(mean)) label append
		}
	}
}	

* Combine MF and FM 
local samp = "( (female==1 & first_opponent_sex==0) | (female==0 & first_opponent_sex==1) )"	
rdrobust run_atall first_margin if `samp', kernel(uni) vce(cluster `cluster_var')
local bw = round(e(h_l), 0.001)

foreach z in run_atall  {	
	
	if "`z'"=="run_atall" local h = `bw'
	if "`z'"=="win_uncond" local h = `bw_win'
	local range = "first_margin>-`h' & first_margin<`h'"

	reg `z' `main_vars_f' `controls' if  `range' & `samp', cluster(`cluster_var') 
	summ `z' if first_loss==0 & e(sample)
	outreg2 using `tabs'/rdllr-`z'-pairs.xls, excel ctitle("MF FM") dec(3) addstat(Mean Winners, r(mean)) label append
}


***********************************
* Heterogeneity by office 
* Table 4
***********************************

foreach y in run_atall   {
	
		forval off=1/5 {
		
			if `off'==1 local c = "County Supervisor"
			if `off'==2 local c = "Mayor"
			if `off'==3 local c = "City Council"
			if `off'==4 local c = "Other City Position"
			if `off'==5 local c = "School Board/Superintendent"												
	
			rdrobust run_atall first_margin if officecat==`off', kernel(uni) vce(cluster `cluster_var')
			local bw = round(e(h_l), 0.001)

			if "`y'"=="run_atall" local h = `bw'
			local range = "first_margin>-`h' & first_margin<`h'"
			
			* Interact female
			reg `y' `main_vars_f' `controls' if  `range' & officecat==`off', cluster(`cluster_var') 
			summ `y' if first_loss==0 & e(sample)
			if `off'==1 outreg2 using `tabs'/rdllr-`y'-female-byoffice.xls, excel ctitle("`c'") dec(3) addstat(Mean Winners, r(mean)) label replace
			if `off'!=1 outreg2 using `tabs'/rdllr-`y'-female-byoffice.xls, excel ctitle("`c'") dec(3) addstat(Mean Winners, r(mean)) label append			
		
		}
}


************************************
* Covariate balance
* Tables A.2, A.3, A.4	
************************************

estimates clear 

foreach y of varlist office_group*  {

	rdrobust `y' first_margin, kernel(uni) vce(cluster `cluster_var')
	local bw = round(e(h_l), 0.001)

	local bw2 = 2*`bw'
	local bwhalf = 0.5*`bw'

	local n = 1
	
	foreach h of numlist `bw' `bw2' `bwhalf' {

		local range = "first_margin>-`h' & first_margin<`h'"

		* All
		reg `y' `main_vars' `controls' if  `range', cluster(`cluster_var')
		summ `y' if first_loss==0 & e(sample)	
		if `h'==`bw' outreg2 using `tabs'/rdllr-`y'-controls-bygender.xls, excel ctitle("All `h'") dec(3) label addstat(Mean Winners, r(mean)) replace 
		if `h'!=`bw' outreg2 using `tabs'/rdllr-`y'-controls-bygender.xls, excel ctitle("All `h'") dec(3) label addstat(Mean Winners, r(mean)) append 
		
		reg `y' `main_vars' `controls' if  `range'
		est store `y'_`n'
		
		local n = `n'+1
	}
}

* joint test
forval x=1/3 {

	suest office_group*_`x', cluster(`cluster_var')
	test first_loss
}

estimates clear 

forval g=0/1 {

	if `g'==0 local gender = "Male"
	if `g'==1 local gender = "Female"

	foreach y of varlist office_group*  {

		rdrobust `y' first_margin if female==`g', kernel(uni) vce(cluster `cluster_var')
		local bw = round(e(h_l), 0.001)

		local bw2 = 2*`bw'
		local bwhalf = 0.5*`bw'

		local n = 1
		
		foreach h of numlist `bw' `bw2' `bwhalf' {

			local range = "first_margin>-`h' & first_margin<`h'"

			reg `y' `main_vars' `controls' if  `range' & female==`g', cluster(`cluster_var')
			summ `y' if first_loss==0 & e(sample)	
			outreg2 using `tabs'/rdllr-`y'-controls-bygender.xls, excel ctitle("`gender' `h'") dec(3) label addstat(Mean Winners, r(mean)) append 

			reg `y' `main_vars' `controls' if  `range' & female==`g'
			est store `y'_`n'
			
			local n = `n'+1
		}
	}

	* joint test
	forval x=1/3 {
		suest office_group*_`x', cluster(`cluster_var')
		test first_loss
	}
	
	estimates clear  
}		
	

foreach y of varlist first_numvotes {

	rdrobust `y' first_margin, kernel(uni) vce(cluster `cluster_var')
	local bw = round(e(h_l), 0.001)

	local bw2 = 2*`bw'
	local bwhalf = 0.5*`bw'

	foreach h of numlist `bw' `bw2' `bwhalf' {

		local range = "first_margin>-`h' & first_margin<`h'"

		* All, by gender
		reg `y' `main_vars' `controls' if  `range', cluster(`cluster_var')
		summ `y' if first_loss==0 & e(sample)	
		if `h'==`bw' outreg2 using `tabs'/rdllr-`y'-controls-bygender.xls, excel ctitle("All `h'") dec(3) label addstat(Mean Winners, r(mean)) replace 
		if `h'!=`bw' outreg2 using `tabs'/rdllr-`y'-controls-bygender.xls, excel ctitle("All `h'") dec(3) label addstat(Mean Winners, r(mean)) append 
	}
}

forval g=0/1 {

	if `g'==0 local gender = "Male"
	if `g'==1 local gender = "Female"

	foreach y of varlist first_numvotes  {

		rdrobust `y' first_margin if female==`g', kernel(uni) vce(cluster `cluster_var')
		local bw = round(e(h_l), 0.001)

		local bw2 = 2*`bw'
		local bwhalf = 0.5*`bw'

		local n = 1
		
		foreach h of numlist `bw' `bw2' `bwhalf' {

			local range = "first_margin>-`h' & first_margin<`h'"

			reg `y' `main_vars' `controls' if  `range' & female==`g', cluster(`cluster_var')
			summ `y' if first_loss==0 & e(sample)	
			outreg2 using `tabs'/rdllr-`y'-controls-bygender.xls, excel ctitle("`gender' `h'") dec(3) label addstat(Mean Winners, r(mean)) append 

			reg `y' `main_vars' `controls' if  `range' & female==`g'
			est store `y'_`n'
			
			local n = `n'+1
		}
	}
}

****************************
* Graphs
****************************

*****************************
* Histogram
* Figure 1		 
*****************************

twoway (histogram first_margin if  female==1, color(green) width(0.02) start(-1) fraction) ///
	(histogram first_margin if  female==0, fcolor(none) lcolor(black) width(0.02) start(-1) fraction ), legend(order(1 "Female" 2 "Male")) ///
	graphregion(color(white)) ///
	saving(`gph'/histogram-marginvictory-mf-multioffice-v2.gph, replace) 
graph export `gph'/histogram-marginvictory-mf-multioffice-v2.pdf, replace 

*******************************************************
* Bin scatter: all individuals pooled (not by gender)
* Figures 2, 4
*******************************************************

* Define range for running variable
local range = "first_margin>-.25 & first_margin<.25"

* Only keep those obs in the range
keep if `range'

* Polynomial degree
local d = 1

gen bin = .
local numbins = 40
local binsize = 0.5/(`numbins')
forval x=-.25(`binsize').2499 {
	local y = `x'+ `binsize'
	replace bin = 0.5*(`x'+`y') if first_margin>=`x' & first_margin<`y'
}
	
foreach y in run_atall win_uncond {

	preserve

	collapse `y', by(bin) 
	drop if bin==.
	
	if "`y'"=="run_atall" {
		summ run_atall
		local max = r(max)
	}

	if "`y'"=="run_atall" local title = "Running Again"
	if "`y'"=="win_uncond" local title = "Winning, Not Conditional on Running"		

	local title2 = "All Offices"
	
	* Run again and conditional winning
	if "`y'"=="run_atall" | "`y'"=="win" twoway (scatter `y' bin, mcolor(black)) /// 
		(lpoly `y' bin if bin<0, lcolor(black) degree(`d')) (lpoly `y' bin if bin>0, lcolor(black) degree(`d')), ///
		xtitle("Margin of Victory") ///
		ytitle("Probability of `title'", margin(0 5 0 0)) xline(0, lcolor(gray) lpattern(dash)) ///
		graphregion(color(white)) legend(off) ylabel(0(0.1)`max') ///		
		saving(`gph'/scatter-`y'-fit.gph, replace)
	if "`y'"=="run_atall" | "`y'"=="win"  graph export	`gph'/scatter-`y'-fit.pdf, replace

	* Unconditional winning
	if "`y'"=="win_uncond" twoway  (scatter `y' bin, msymbol(black) mcolor(black)) /// 
		(lpoly `y' bin if bin<0, lcolor(black)  degree(`d')) (lpoly `y' bin if bin>0, lcolor(black) degree(`d')), ///
		xtitle("Margin of Victory") ///
		ytitle("Probability of `title'", margin(0 5 0 0)) xline(0, lcolor(gray) lpattern(dash)) ///
		graphregion(color(white)) legend(off) ylabel(0(0.1)`max') ///		
		saving(`gph'/scatter-`y'-fit.gph, replace)
	if "`y'"=="win_uncond" graph export 	`gph'/scatter-`y'-fit.pdf, replace
		
	restore
}

* Other covariates
foreach y of varlist office_group* medcity {

	preserve

	collapse `y', by(bin) 
	drop if bin==.

	summ `y'
	local max = r(max)

	local title2 = "All Offices"
	
	twoway (scatter `y' bin, mcolor(black)) /// 
		(lpoly `y' bin if bin<0, lcolor(black) degree(`d')) (lpoly `y' bin if bin>0, lcolor(black) degree(`d')), ///
		xtitle("Margin of Victory") ///
		xline(0, lcolor(gray) lpattern(dash)) ///
		graphregion(color(white)) legend(off) ylabel(0(0.1)`max') ///		
		saving(`gph'/scatter-`y'-fit.gph, replace)
	if "`y'"=="run_atall" | "`y'"=="win"  graph export	`gph'/scatter-`y'-fit.pdf, replace

	restore
}

*****************************
* Bin scatter: by gender
* Figure 2 
*****************************
	
foreach y in run_atall win_uncond {

	preserve

	collapse `y', by(bin female)
	drop if bin==.
	
	if "`y'"=="run_atall" local title = "Running Again"
	if "`y'"=="win_uncond" local title = "Winning, Not Conditional on Running"		

	local title2 = "All Offices"
	
	* Run again and conditional winning
	if "`y'"=="run_atall" | "`y'"=="win" twoway  (scatter `y' bin if female==1, msymbol(D) mcolor(green)) (scatter `y' bin if female==0, msymbol(Oh) mcolor(black)), /// 
		legend( label(1 Female) label(2 Male)) xtitle("Margin of Victory") ytitle("Probability of `title'", margin(0 5 0 0)) xline(0, lcolor(gray) lpattern(dash)) ///
		graphregion(color(white)) ///
		saving(`gph'/scatter-`y'-bysex.gph, replace)
	if "`y'"=="run_atall" | "`y'"=="win"  graph export	`gph'/scatter-`y'-bysex.pdf, replace

	if "`y'"=="run_atall" | "`y'"=="win" twoway  (scatter `y' bin if female==1, msymbol(D) mcolor(green)) (scatter `y' bin if female==0, msymbol(Oh) mcolor(black)) /// 
		(lpoly `y' bin if female==1 & bin<0, lcolor(green)  degree(`d')) (lpoly `y' bin if female==1 & bin>0, lcolor(green) degree(`d')) ///
		(lpoly `y' bin if female==0 & bin<0, lcolor(black) degree(`d')) (lpoly `y' bin if female==0 & bin>0, lcolor(black) degree(`d')), ///
		legend( label(1 Female) label(2 Male) order(1 2)) xtitle("Margin of Victory") ///
		ytitle("Probability of `title'", margin(0 5 0 0)) xline(0, lcolor(gray) lpattern(dash)) ///
		graphregion(color(white)) ///		
		saving(`gph'/scatter-`y'-bysex-fit.gph, replace)
	if "`y'"=="run_atall" | "`y'"=="win"  graph export	`gph'/scatter-`y'-bysex-fit.pdf, replace

	* Unconditional winning
	if "`y'"=="win_uncond" twoway  (scatter `y' bin if female==1, msymbol(D) mcolor(green)) (scatter `y' bin if female==0, msymbol(Oh) mcolor(black)), /// 
		legend( label(1 Female) label(2 Male)) xtitle("Margin of Victory") ytitle("Probability of `title'", margin(0 5 0 0)) xline(0, lcolor(gray) lpattern(dash)) ///
		graphregion(color(white)) ///		
		saving(`gph'/scatter-`y'-bysex.gph, replace)
	if "`y'"=="win_uncond" graph export 	`gph'/scatter-`y'-bysex.pdf, replace

	if "`y'"=="win_uncond" twoway  (scatter `y' bin if female==1, msymbol(D) mcolor(green)) (scatter `y' bin if female==0, msymbol(Oh) mcolor(black)) /// 
		(lpoly `y' bin if female==1 & bin<0, lcolor(green) degree(`d')) (lpoly `y' bin if female==1 & bin>0, lcolor(green) degree(`d')) ///
		(lpoly `y' bin if female==0 & bin<0, lcolor(black) degree(`d')) (lpoly `y' bin if female==0 & bin>0, lcolor(black) degree(`d')), ///
		legend( label(1 Female) label(2 Male) order(1 2)) xtitle("Margin of Victory") ///
		ytitle("Probability of `title'", margin(0 5 0 0)) xline(0, lcolor(gray) lpattern(dash)) ///
		graphregion(color(white)) ///		
		saving(`gph'/scatter-`y'-bysex-fit.gph, replace)
	if "`y'"=="win_uncond" graph export 	`gph'/scatter-`y'-bysex-fit.pdf, replace
	
	restore
}

***********************************
* Heterogeneity by office 
* Figure 3
***********************************

local numbins = 30
local binsize = 0.5/(`numbins')
forval x=-.25(`binsize').2499 {
	local y = `x'+ `binsize'
	replace bin = 0.5*(`x'+`y') if first_margin>=`x' & first_margin<`y'
}

foreach off of numlist 3 5 {
		
		foreach y in run_atall win_uncond {

			preserve
			keep if officecat==`off'

			collapse `y', by(bin female)
			drop if bin==.
			
			if "`y'"=="run_atall" {
				summ run_atall
				local max = r(max)
			}
	
			if "`y'"=="run_atall" local title = "Running Again"
			if "`y'"=="win_uncond" local title = "Winning, Not Conditional on Running"		

			local title2 = "`c' All Offices"

			if `off'==1 local c = "County Supervisor"
			if `off'==2 local c = "Mayor"
			if `off'==3 local c = "City Council"
			if `off'==4 local c = "Other City Position"
			if `off'==5 local c = "School Board/Superintendent"																
	
			* Run again and conditional winning
			if "`y'"=="run_atall" | "`y'"=="win" twoway  (scatter `y' bin if female==1, msymbol(D) mcolor(green)) (scatter `y' bin if female==0, msymbol(Oh) mcolor(black)) /// 
				(lpoly `y' bin if female==1 & bin<0, lcolor(green) degree(`d')) (lpoly `y' bin if female==1 & bin>0, lcolor(green) degree(`d')) ///
				(lpoly `y' bin if female==0 & bin<0, lcolor(black) degree(`d')) (lpoly `y' bin if female==0 & bin>0, lcolor(black) degree(`d')), ///
				legend( label(1 Female) label(2 Male) order(1 2)) xtitle("Margin of Victory") ///
				ytitle("Probability of `title'", margin(0 5 0 0)) xline(0, lcolor(gray) lpattern(dash)) ///
				graphregion(color(white))  ylabel(0(0.1)`max') ///		
				saving(`gph'/scatter-`y'-bysex-fit-office`off'.gph, replace)
			if "`y'"=="run_atall" | "`y'"=="win" graph export 	`gph'/scatter-`y'-bysex-fit-office`off'.pdf, replace

			* Unconditional winning
			if "`y'"=="win_uncond" twoway  (scatter win bin if female==1, msymbol(D) mcolor(green)) (scatter win bin if female==0, msymbol(Oh) mcolor(black)) /// 
				(lpoly win bin if female==1 & bin<0, lcolor(green) degree(`d')) (lpoly win bin if female==1 & bin>0, lcolor(green) degree(`d')) ///
				(lpoly win bin if female==0 & bin<0, lcolor(black) degree(`d')) (lpoly win bin if female==0 & bin>0, lcolor(black) degree(`d')), ///
				legend( label(1 Female) label(2 Male) order(1 2)) xtitle("Margin of Victory") ///
				ytitle("Probability of `title'", margin(0 5 0 0)) xline(0, lcolor(gray) lpattern(dash)) ///
				graphregion(color(white))  ylabel(0(0.1)`max') ///
				saving(`gph'/scatter-`y'-bysex-fit-office`off'.gph, replace)	
			if "`y'"=="win_uncond" graph export `gph'/scatter-`y'-bysex-fit-office`off'.pdf, replace
	
			restore
		}

}


}

*****************************
* McCrary Test
* Figure A.4		 
*****************************

use ca-election, clear

* using the code from DCdensity_example.do available on McCrary's website
* https://eml.berkeley.edu/~jmccrary/DCdensity/

* All
DCdensity first_margin, breakpoint(0) generate(Xj Yj r0 fhat se_fhat) nograph
local h=r(bandwidth)/2
drop Xj Yj r0 fhat se_fhat
DCdensity first_margin, breakpoint(0) h(`h') generate(Xj Yj r0 fhat se_fhat) 
graph save `gph'/mccrary-all.gph, replace
drop Xj Yj r0 fhat se_fhat

* Male
DCdensity first_margin if female==0, breakpoint(0) generate(Xj Yj r0 fhat se_fhat) nograph
local h=r(bandwidth)/2
drop Xj Yj r0 fhat se_fhat
DCdensity first_margin if female==0, breakpoint(0) h(`h') generate(Xj Yj r0 fhat se_fhat) 
graph save `gph'/mccrary-m.gph, replace
drop Xj Yj r0 fhat se_fhat

* Female
DCdensity first_margin if female==1, breakpoint(0) generate(Xj Yj r0 fhat se_fhat) nograph
local h=r(bandwidth)/2
drop Xj Yj r0 fhat se_fhat
DCdensity first_margin if female==1, breakpoint(0) h(`h') generate(Xj Yj r0 fhat se_fhat) 
graph save `gph'/mccrary-f.gph, replace
drop Xj Yj r0 fhat se_fhat

log close
